<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="产品编码" prop="productCode">
        <el-select
        filterable
          v-model="queryParams.productCode"
          placeholder="请选择产品编码"
          style="width: 200px"
        >
          <el-option
            v-for="dict in productCodeOptions"
            :key="dict.modelCode"
            :label="dict.modelDesLocal"
            :value="dict.modelCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工序编码" prop="operationCode">
        <el-input
          v-model="queryParams.operationCode"
          placeholder="请输入工序编码"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工序名称" prop="operationName">
        <el-input
          v-model="queryParams.operationName"
          placeholder="请输入工序名称"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺路线编码" prop="routingCode" label-width="100px">
        <el-input
          v-model="queryParams.routingCode"
          placeholder="请输入工艺路线编码"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺路线名称" prop="routingName" label-width="100px">
        <el-input
          v-model="queryParams.routingName"
          placeholder="请输入工艺路线名称"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车间编码" prop="workShopCode">
        <el-select
          v-model="queryParams.workShopCode"
          placeholder="请选择车间编码"
          style="width: 200px"
          filterable
        >
          <el-option
            v-for="item in workShopCodeOptions1"
            :key="item.shopCode"
            :label="item.shopName"
            :value="item.shopCode"
          >
            <span style="float: left">{{ item.shopCode }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.shopName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产线编码" prop="lineCode">
        <el-select
          v-model="queryParams.lineCode"
          placeholder="请选择产线编码"
          style="width: 200px"
          filterable
        >
          <el-option
            v-for="item in lineCodeOptions1"
            :key="item.shopCode"
            :label="item.shopName"
            :value="item.shopCode"
          >
            <span style="float: left">{{ item.shopCode }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.shopName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工位编码" prop="stationCode">
        <el-select
          v-model="queryParams.stationCode"
          placeholder="请选择工位编码"
          style="width: 200px"
          filterable
        >
          <el-option
            v-for="item in stationCodeOptions1"
            :key="item.shopCode"
            :label="item.shopName"
            :value="item.shopCode"
          >
            <span style="float: left">{{ item.shopCode }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.shopName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:operations:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:operations:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:operations:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:operations:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="operationsList"
      @selection-change="handleSelectionChange"
      style="width: 100%"
    >
      <el-table-column type="selection" width="60" align="center" fixed="left" />
      <el-table-column label="主键" align="center" prop="id" width="100" fixed="left" v-if="false" />
      <el-table-column label="产品编码" align="center" prop="productCode" width="100" fixed="left" />
      <el-table-column label="工序编码" align="center" prop="operationCode" width="100" />
      <el-table-column label="工序名称" align="center" prop="operationName" width="200" />
      <el-table-column label="工序序号" align="center" prop="operationNum" width="100" />
      <el-table-column label="工艺路线编码" align="center" prop="routingCode" width="100" />
      <el-table-column label="工艺路线名称" align="center" prop="routingName" width="200" />
      <el-table-column label="车间" align="center" prop="workShopCode" :formatter="workShopCodeFormat" width="100" />
      <el-table-column label="产线" align="center" prop="lineCode" :formatter="lineCodeFormat" width="100" />
      <el-table-column label="工位" align="center" prop="stationCode" :formatter="stationCodeFormat" width="100" />
      <el-table-column label="技术要求" align="center" prop="technicalRequire" width="100" />
      <el-table-column label="节拍时间(S)" align="center" prop="beatTime" width="120" />
      <el-table-column label="装配图示" align="center" prop="doWorkPicture" width="100" />
      <el-table-column label="工艺卡" align="center" prop="operationCard" width="100" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="280" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-guide"
            style="margin-right: 9px"
            @click="handleEditSteps(scope.row)"
            v-hasPermi="['base:operations:guide']"
          >工步设置</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            style="margin-right: 9px"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:operations:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:operations:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工序对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="760px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="产品名称" prop="productCode">
              <el-select
                v-model="form.productCode"
                filterable 
                placeholder="请选择产品名称"
                style="width: 240px"
              >
                <el-option
                  v-for="dict in productCodeOptions"
                  :key="dict.modelCode"
                  :label="dict.modelDesLocal"
                  :value="dict.modelCode"
                >
                  <span style="float: left">{{ dict.modelCode }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ dict.modelDesLocal }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工序编码" prop="operationCode">
              <el-input
                v-model="form.operationCode"
                placeholder="请输入工序编码"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工序名称" prop="operationName">
              <el-input
                v-model="form.operationName"
                placeholder="请输入工序名称"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工序序号" prop="operationNum">
              <el-input
                v-model="form.operationNum"
                placeholder="请输入工序序号"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工艺路线编码" prop="routingCode">
              <el-input
                v-model="form.routingCode"
                placeholder="请输入工艺路线编码"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工艺路线名称" prop="routingName">
              <el-input
                v-model="form.routingName"
                placeholder="请输入工艺路线名称"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="车间" prop="workShopCode">
              <el-select
                v-model="form.workShopCode"
                placeholder="请选择车间"
                style="width: 240px"
                filterable
                @change="Change"
              >
                <el-option
                  v-for="item in workShopCodeOptions"
                  :key="item.shopCode"
                  :label="item.shopName"
                  :value="item.shopCode"
                >
                  <span style="float: left">{{ item.shopCode }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.shopName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="产线" prop="lineCode">
              <el-select
                v-model="form.lineCode"
                placeholder="请选择产线"
                style="width: 240px"
                filterable
                @change="Change1"
              >
                <el-option
                  v-for="item in lineCodeOptions"
                  :key="item.shopCode"
                  :label="item.shopName"
                  :value="item.shopCode"
                >
                  <span style="float: left">{{ item.shopCode }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.shopName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工位" prop="stationCode">
              <el-select
                v-model="form.stationCode"
                placeholder="请选择工位"
                style="width: 240px"
                filterable
              >
                <el-option
                  v-for="item in stationCodeOptions"
                  :key="item.shopCode"
                  :label="item.shopName"
                  :value="item.shopCode"
                >
                  <span style="float: left">{{ item.shopCode }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.shopName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="节拍时间(S)" prop="beatTime">
              <el-input
                v-model="form.beatTime"
                placeholder="请输入节拍时间(S)"
                style="width: 240px"
                type="number"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="装配图示" prop="doWorkPicture">
              <el-input
                v-model="form.doWorkPicture"
                placeholder="请输入装配图示"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工艺卡" prop="operationCard">
              <el-input
                v-model="form.operationCard"
                placeholder="请输入工艺卡"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="技术要求" prop="technicalRequire">
              <el-input
                v-model="form.technicalRequire"
                type="textarea"
                :rows="4"
                placeholder="请输入技术要求"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOperations,
  getOperations,
  delOperations,
  addOperations,
  updateOperations,
  exportOperations,
} from "@/api/base/operations";
import { listAllSetModel } from "@/api/parameter/set_model";
import { getShopByType, getShop } from "@/api/base/shop.js";
export default {
  name: "Operations",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工序表格数据
      operationsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 产品编码字典
      productCodeOptions: [],
      // 车间编码字典
      workShopCodeOptions: [],
      // 产线编码字典
      lineCodeOptions: [],
      // 工位编码字典
      stationCodeOptions: [],
      // 车间编码字典
      workShopCodeOptions1: [],
      // 产线编码字典
      lineCodeOptions1: [],
      // 工位编码字典
      stationCodeOptions1: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productCode: undefined,
        operationCode: undefined,
        operationName: undefined,
        routingCode: undefined,
        routingName: undefined,
        workShopCode: undefined,
        lineCode: undefined,
        stationCode: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productCode: [{ required: true, message: "产品名称不能为空", trigger: "change" }],
        operationCode: [{ required: true, message: "工序编码不能为空", trigger: "blur" }],
        operationName: [{ required: true, message: "工序名称不能为空", trigger: "blur" }],
        operationNum: [{ required: true, message: "工序序号不能为空", trigger: "blur" }],
        routingCode: [{ required: true, message: "工艺路线编码不能为空", trigger: "blur" }],
        routingName: [{ required: true, message: "工艺路线名称不能为空", trigger: "blur" }],
        workShopCode: [{ required: true, message: "车间不能为空", trigger: "change" }],
        lineCode: [{ required: true, message: "产线不能为空", trigger: "change" }],
        stationCode: [{ required: true, message: "工位不能为空", trigger: "change" }]
      },
    };
  },
  created() {
    this.getList();
    listAllSetModel().then((res) => {
      if (res.code == 200) {
        this.productCodeOptions = res.data;
      }
    });
    // 列表数据查询
    var shoptype = "2";
    getShopByType(shoptype).then((res) => {
      this.workShopCodeOptions = res.data;
      this.workShopCodeOptions1 = res.data;
    });
    // 列表数据查询
    var shoptype = "3";
    getShopByType(shoptype).then((res) => {
      this.lineCodeOptions = res.data;
      this.lineCodeOptions1 = res.data;
    });
    // 列表数据查询
    var shoptype = "4";
    getShopByType(shoptype).then((res) => {
      this.stationCodeOptions = res.data;
      this.stationCodeOptions1 = res.data;
    });
  },
  methods: {
    //下拉联动
    Change(e) {
      // 根据id进行筛选（id是唯一的，如果获取多个，需要自己重新定义筛选条件）
      let item = this.workShopCodeOptions.filter((options) => {
        return options.shopCode === e;
      });
      this.lineCodeOptions=this.lineCodeOptions1;
      this.form.stationCode="";
      this.form.lineCode="";
      // 根据id进行筛选（id是唯一的，如果获取多个，需要自己重新定义筛选条件）
      this.lineCodeOptions = this.lineCodeOptions.filter((options) => {
        return options.parentId === item[0].shopId;
      });
    },
    //下拉联动
    Change1(e) {
      // 根据id进行筛选（id是唯一的，如果获取多个，需要自己重新定义筛选条件）
      let item = this.lineCodeOptions.filter((options) => {
        return options.shopCode === e;
      });
      this.stationCodeOptions=this.stationCodeOptions1;
      this.form.stationCode="";
      // 根据id进行筛选（id是唯一的，如果获取多个，需要自己重新定义筛选条件）
      this.stationCodeOptions = this.stationCodeOptions.filter((options) => {
        return options.parentId === item[0].shopId;
      });
    },
    /** 查询工序列表 */
    getList() {
      this.loading = true;
      listOperations(this.queryParams).then((response) => {
        this.operationsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // // 产品编码字典翻译
    // productCodeFormat(row, column) {
    //   return this.selectDictLabel(this.productCodeOptions, row.productCode);
    // },
    // 车间编码字典翻译
    workShopCodeFormat(row, column) {
      //车间翻译
      var actions = [];
      Object.keys(this.workShopCodeOptions).some((key) => {
        if (this.workShopCodeOptions[key].shopCode == "" + row.workShopCode) {
          actions.push(this.workShopCodeOptions[key].shopName);
          return true;
        }
      });
      return actions.join(" ");
    },
    // 产线编码字典翻译
    lineCodeFormat(row, column) {
      //产线翻译
      var actions = [];
      Object.keys(this.lineCodeOptions).some((key) => {
        if (this.lineCodeOptions[key].shopCode == "" + row.lineCode) {
          actions.push(this.lineCodeOptions[key].shopName);
          return true;
        }
      });
      return actions.join(" ");
    },
    // 工位编码字典翻译
    stationCodeFormat(row, column) {
      //产线翻译
      var actions = [];
      Object.keys(this.stationCodeOptions).some((key) => {
        if (this.stationCodeOptions[key].shopCode == "" + row.stationCode) {
          actions.push(this.stationCodeOptions[key].shopName);
          return true;
        }
      });
      return actions.join(" ");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工序";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOperations(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工序";
      });
    },
    handleEditSteps(row) {
      const stationCode = row.stationCode;
      const operationCode = row.operationCode;
      this.$router.push(`/operations/steps/${stationCode}/${operationCode}`);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateOperations(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addOperations(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除工序编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delOperations(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有工序数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportOperations(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        })
        .catch(function () {});
    },
  },
};
</script>